{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# python मा selenium को प्रयोग browser automation अथ\n", " वा ब्राउज रलाई आँफै चलाउन प्रयोग हुन्छ।  तेस्तै आज हामी python प्रयोग गरेर wifirouter reboot गर्न प्रयोग गर्ने छौ. सबैभन्दा पहिला तपाईंको सिस्टममा python install हुनुपर्छ तेस्तै अब तपाईं https://chromedriver.chromium.org/downloads/ मा गएर आफ्नो chrome भर्सन अनुसार ड्राइभर download गर्नुस्। अब त्यो फाइललाई unzip गरेर तेस्भित्रको फाइललाई तपाईंले सम्झिने फोल्डरमा राख्नुहोस्। \n",
    "अब cmd कमाण्डलाइनमा गएर pip install selenium टाईप गर्नुहोस् र केहीबेर मै सेलेनियम install हुनेछ। \n",
    "\n",
    "अब एउटा फोल्डर बनाउनुहोस् र तपाईंको code editor मा open गर्नुहोस्।\n",
    "हामी main.py नामको फाइल बनाउछौ। अब तेस्भित्र यि कुराहरु लेख्छौ: "
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "from selenium import webdriver\n",
    "\n",
    "browser = webdriver.Chrome()\n",
    "browser.get('https://facebook.com')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "browser.get('http://192.168.1.1/login')"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "username = 'admin'\n",
    "password = 'admin'\n",
    "    \n",
    "user_field = browser.find_element_by_name('sth')\n",
    "pass_field = browser.find_element_by_name('sth')\n",
    "user_field.send_keys(username)\n",
    "pass_field.send_keys(password) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "browser.find_element_by_css_selector(\"input[onclick*='return saveChanges()']\").click()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "browser.get('http://192.168.1.1/reboot.htm/')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "browser.find_element_by_css_selector(\"input[onclick*='return rebootClick()']\").click()\n",
    "alert_obj = browser.switch_to.alert\n",
    "time.sleep(2)\n",
    "alert_obj.accept() "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from win_notify import ballon_tip as winnotifier"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "winnotifier(\"sharmaji: Wifi rebooted\",\\\n",
    "     \"TOTOLINK succesfully has been rebooted wait for 10-15 secs\")\n",
    "time.sleep(3)\n",
    "browser.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "chrome_options = webdriver.ChromeOptions()\n",
    "chrome_options.add_argument(\"--incognito\")\n",
    "chrome_options.add_argument(\"--disable-extensions\")\n",
    "chrome_options.add_argument(\"--disable-gpu\")\n",
    "chrome_options.add_argument(\"--headless\")\n",
    "browser = webdriver.Chrome(options=chrome_options) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def setup(incognito=False):\n",
    "    global browser\n",
    "    if incognito:\n",
    "        chrome_options = webdriver.ChromeOptions()\n",
    "        chrome_options.add_argument(\"--incognito\")\n",
    "        chrome_options.add_argument(\"--disable-extensions\")\n",
    "        chrome_options.add_argument(\"--disable-gpu\")\n",
    "        chrome_options.add_argument(\"--headless\")\n",
    "        browser = webdriver.Chrome(options=chrome_options)\n",
    "    else:\n",
    "        browser = webdriver.Chrome()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def authenticate(router_user, router_pass):"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def authenticate(router_user, router_pass):\n",
    "    browser.get(\"http://192.168.1.1/login.htm\")\n",
    "    user_field = browser.find_element_by_name('username')\n",
    "    pass_field = browser.find_element_by_name('userpass')\n",
    "    user_field.send_keys(router_user)\n",
    "    pass_field.send_keys(router_pass)\n",
    "    browser.find_element_by_css_selector(\"input[onclick*='return saveChanges()']\").click()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def reboot():\n",
    "    browser.get(\"http://192.168.1.1/reboot.htm\")\n",
    "    time.sleep(1)\n",
    "    browser.find_element_by_css_selector(\"input[onclick*='return rebootClick()']\").click()\n",
    "    alert_obj = browser.switch_to.alert\n",
    "    time.sleep(2)\n",
    "    alert_obj.accept()\n",
    "    winnotifier(\"sharmaji: Wifi rebooted\",\\\n",
    "     \"TOTOLINK succesfully has been rebooted wait for 10-15 secs\")\n",
    "    time.sleep(3)\n",
    "    browser.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "setup(incognito=True)\n",
    "try:\n",
    "    authenticate(\"admin\", \"admin\")\n",
    "    reboot()\n",
    "except:\n",
    "    reboot()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "from selenium import webdriver\n",
    "from win_notify import balloon_tip as winnotifier\n",
    " \n",
    " \n",
    "def setup(incognito=False):\n",
    "    global browser\n",
    "    if incognito:\n",
    "        chrome_options = webdriver.ChromeOptions()\n",
    "        chrome_options.add_argument(\"--incognito\")\n",
    "        chrome_options.add_argument(\"--disable-extensions\")\n",
    "        chrome_options.add_argument(\"--disable-gpu\")\n",
    "        chrome_options.add_argument(\"--headless\")\n",
    "        browser = webdriver.Chrome(options=chrome_options)\n",
    "    else:\n",
    "        browser = webdriver.Chrome()\n",
    " \n",
    "def authenticate(\n",
    "        router_user, router_pass, element_name=\"username\",\n",
    "        element_password=\"userpass\", button_onclick='return saveChanges()'):\n",
    " \n",
    "    browser.get(\"http://192.168.1.1/login.htm\")\n",
    "    username = browser.find_element_by_name(element_name)\n",
    "    password = browser.find_element_by_name(element_password)\n",
    "    username.send_keys(router_user)\n",
    "    password.send_keys(router_pass)\n",
    "    browser.find_element_by_css_selector(\"input[onclick*='return saveChanges()']\").click()\n",
    " \n",
    "def reboot():\n",
    "    browser.get(\"http://192.168.1.1/reboot.htm\")\n",
    "    time.sleep(1)\n",
    "    browser.find_element_by_css_selector(\"input[onclick*='return rebootClick()']\").click()\n",
    "    alert_obj = browser.switch_to.alert\n",
    "    time.sleep(2)\n",
    "    alert_obj.accept()\n",
    "    winnotifier(\"sharmaji: Wifi rebooted\",\\\n",
    "     \"TOTOLINK succesfully has been rebooted wait for 10-15 secs\")\n",
    "    time.sleep(3)\n",
    "    browser.close()\n",
    " \n",
    "setup(incognito=True)\n",
    "try:\n",
    "    authenticate(\"admin\", \"admin\")\n",
    "    reboot()\n",
    "except:\n",
    "    reboot()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
